package com.pennywise.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * 目录结构初始化器
 * 负责创建应用所需的所有目录和初始化文件
 */
@Slf4j
@Component
@Order(1)  // 确保最先执行
public class DirectoryInitializer {

    private static final String BASE_DIR = System.getProperty("user.dir") + "/data";
    
    private static final List<String> REQUIRED_DIRS = Arrays.asList(
        "/db",          // 数据库文件目录
        "/logs",        // 日志文件目录
        "/exports",     // 导出文件目录
        "/backup",      // 备份文件目录
        "/temp"         // 临时文件目录
    );
    
    @PostConstruct
    public void initialize() {
        log.info("开始初始化目录结构");
        
        // 创建必要的目录
        REQUIRED_DIRS.forEach(dir -> {
            File directory = new File(BASE_DIR + dir);
            if (!directory.exists()) {
                boolean created = directory.mkdirs();
                if (created) {
                    log.info("创建目录: {}", directory.getAbsolutePath());
                } else {
                    log.error("创建目录失败: {}", directory.getAbsolutePath());
                    throw new RuntimeException("无法创建必要的目录: " + directory.getAbsolutePath());
                }
            }
        });
        
        log.info("目录结构初始化完成");
    }
} 